//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.pc_rfid.api;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Vector;

public class PC_NET extends PC_COMM {
    public static final int ReceiveBufferSize = 1024;
    private static final String TAG = "PC_NET";
    private DatagramSocket sock = null;
    private InetAddress IPaddr = null;
    private int Port;
    private int ReceiveLen;
    private byte[] SendBuffer = null;
    private byte[] ReceiveBuffer = null;
    private Vector<byte[]> ReceDataArray = null;

    public PC_NET(String ip, int port) throws SocketException, UnknownHostException {
        this.open(ip, port);
    }

    public int open(String ip, int port) throws SocketException, UnknownHostException {
        if (!this.isOpen) {
            this.sock = new DatagramSocket();
            this.sock.setSoTimeout(2000);
            this.ReceiveBuffer = new byte[1024];
            this.isOpen = true;
        }

        this.IPaddr = InetAddress.getByName(ip);
        this.Port = port;
        return 0;
    }

    public synchronized int Send(byte[] data) {
        if (!this.isOpen) {
            return -1;
        } else {
            this.clearBuffer();
            this.SendBuffer = data;
            (new Thread(new Runnable() {
                public void run() {
                    synchronized(PC_NET.this) {
                        DatagramPacket Sendpack = new DatagramPacket(PC_NET.this.SendBuffer, (PC_NET.this.SendBuffer[1] + 256) % 256 + 1, PC_NET.this.IPaddr, PC_NET.this.Port);

                        try {
                            PC_NET.this.sock.send(Sendpack);
                        } catch (IOException var5) {
                        }

                        PC_NET.this.notifyAll();
                    }
                }
            })).start();

            try {
                this.wait();
            } catch (InterruptedException var3) {
                var3.printStackTrace();
            }

            return 0;
        }
    }

    public synchronized int Receive(byte[] data) {
        if (!this.isOpen) {
            return -1;
        } else {
            (new Thread(new Runnable() {
                public void run() {
                    synchronized(PC_NET.this) {
                        DatagramPacket Receivepack = new DatagramPacket(PC_NET.this.ReceiveBuffer, PC_NET.this.ReceiveBuffer.length);

                        try {
                            PC_NET.this.sock.receive(Receivepack);
                            PC_NET.this.ReceiveLen = Receivepack.getLength();
                        } catch (IOException var5) {
                        }

                        PC_NET.this.notifyAll();
                    }
                }
            })).start();

            try {
                this.wait();
            } catch (InterruptedException var3) {
                var3.printStackTrace();
            }

            if (this.ParsePack(this.ReceiveBuffer, this.ReceiveLen, data)) {
                Arrays.fill(this.ReceiveBuffer, (byte)0);
                int len = (data[1] + 256) % 256 + 1;
                return len;
            } else {
                Arrays.fill(this.ReceiveBuffer, (byte)0);
                return 0;
            }
        }
    }

    public int close() {
        if (this.isOpen) {
            this.isOpen = false;
            this.sock.close();
            this.sock = null;
        }

        return 0;
    }

    public void clearBuffer() {
        this.clearParse();
    }

    public int Send_Receive(RFdata rf) {
        if (!this.isOpen) {
            return -1;
        } else {
            int len = 0;
            if (this.Send(rf.SendData) == 0) {
                len = this.Receive(rf.RecvData);
            }

            return len;
        }
    }

    private boolean NetInventoryACK(byte[] data, short NumFlag) {
        data[0] = -86;
        data[1] = 8;
        data[2] = -1;
        data[3] = -6;
        data[4] = 1;
        data[5] = (byte)(NumFlag >> 8 & 255);
        data[6] = (byte)(NumFlag & 255);
        int crc = PC_COMM.crc16(data);
        data[data[1] - 1] = (byte)(crc % 256);
        data[data[1]] = (byte)(crc / 256);
        return true;
    }

    public synchronized int NETinventoryTag(Vector<byte[]> rece, byte[] send) {
        if (!this.isOpen) {
            return -1;
        } else {
            this.ReceDataArray = rece;
            this.SendBuffer = send;
            (new Thread(new Runnable() {
                public void run() {
                    synchronized(PC_NET.this) {
                        DatagramPacket Sendpack = new DatagramPacket(PC_NET.this.SendBuffer, (PC_NET.this.SendBuffer[1] + 256) % 256 + 1, PC_NET.this.IPaddr, PC_NET.this.Port);
                        DatagramPacket Receivepack = new DatagramPacket(PC_NET.this.ReceiveBuffer, PC_NET.this.ReceiveBuffer.length);

                        try {
                            PC_NET.this.sock.send(Sendpack);
                            PC_NET.this.sock.receive(Receivepack);
                            PC_NET.this.ReceiveLen = Receivepack.getLength();
                            PC_NET.this.ReceDataArray.add(new byte[256]);
                            if (PC_NET.this.ParsePack(PC_NET.this.ReceiveBuffer, PC_NET.this.ReceiveLen, (byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))) {
                                int len = (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[1] + 256) % 256;
                                byte[] respond = new byte[256];
                                int flag = (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[6] + 256) % 256 * 256 + (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[7] + 256) % 256;
                                PC_NET.this.NetInventoryACK(respond, (short)flag);
                                DatagramPacket Respondpack = new DatagramPacket(respond, 9, PC_NET.this.IPaddr, PC_NET.this.Port);
                                PC_NET.this.sock.send(Respondpack);

                                while(len > 9 && len != 0) {
                                    len = 0;
                                    PC_NET.this.sock.receive(Receivepack);
                                    PC_NET.this.ReceiveLen = Receivepack.getLength();
                                    PC_NET.this.ReceDataArray.add(new byte[256]);
                                    if (PC_NET.this.ParsePack(PC_NET.this.ReceiveBuffer, PC_NET.this.ReceiveLen, (byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))) {
                                        len = (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[1] + 256) % 256;
                                        respond = new byte[256];
                                        flag = (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[6] + 256) % 256 * 256 + (((byte[])PC_NET.this.ReceDataArray.get(PC_NET.this.ReceDataArray.size() - 1))[7] + 256) % 256;
                                        PC_NET.this.NetInventoryACK(respond, (short)flag);
                                        Respondpack = new DatagramPacket(respond, 9, PC_NET.this.IPaddr, PC_NET.this.Port);
                                        PC_NET.this.sock.send(Respondpack);
                                    }
                                }
                            }
                        } catch (IOException var9) {
                            var9.printStackTrace();
                        }

                        PC_NET.this.notifyAll();
                    }
                }
            })).start();

            try {
                this.wait();
            } catch (InterruptedException var4) {
                var4.printStackTrace();
            }

            return this.ReceDataArray.size();
        }
    }
}
